<?php 
	//Get post by post-id
	function get_post_by_id($id)
	{
		$query = "SELECT * FROM posts WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		$row = array();
		$num_row = mysql_num_rows($result);
		if ($num_row !== 0)
			$row = mysql_fetch_array($result);
		return $row;
	}
	//Get post by post-state
	function get_posts_by_state($state)
	{
		$query = "SELECT * FROM posts as p WHERE p.state=$state  ORDER BY created_on";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Get posts in category
	function get_posts_by_cat($cat_id)
	{
		$query = "SELECT * FROM posts WHERE cat_id=$cat_id  ORDER BY created_on";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Count posts with conditions
	function count_posts_with_conditions($conditions)
	{
		$query = "SELECT COUNT(id) FROM posts as p $conditions";
		$result = mysql_query($query) or die(mysql_error());
		$count = mysql_result($result, 0);
		return $count;
	}
	//Get posts with conditions
	function get_posts_with_conditions($from,$posts_per_page,$conditions)
	{
		$query = "SELECT p.id,title,content,c.category_name,post_meta,post_robots,meta_title,meta_description,p.created_by,p.created_on,p.state FROM posts as p 
			LEFT JOIN categories as c ON p.cat_id = c.id $conditions ORDER BY created_on LIMIT $from,$posts_per_page";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Get all post
	function get_all_posts($from,$posts_per_page)
	{
		$query = "SELECT p.id,title,content,c.category_name,post_meta,post_robots,meta_title,meta_description,p.created_by,p.created_on,p.state FROM posts as p 
			LEFT JOIN categories as c ON p.cat_id = c.id ORDER BY created_on LIMIT $from,$posts_per_page";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Get post by post-id
	function insert_post($title,$content,$cat_id,$state,$post_meta,$post_robots,$meta_title,$meta_description,$created_by)
	{
		$query = "INSERT INTO posts (title,content,cat_id,state,post_meta,post_robots,meta_title,meta_description,created_by,created_on) 
					VALUES ('$title','$content',$cat_id,$state,'$post_meta','$post_robots','$meta_title','$meta_description','$created_by',now())";
		$result = mysql_query($query) or die(mysql_error());
		$id = mysql_insert_id();
		if ($id !== false && $id !== 0)
			return $id;
		return 0;
	}
	//Update post state
	function update_posts_state($conditions,$value)
	{
		$query = "UPDATE posts SET $value WHERE $conditions";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();	
	}
	//Update post by post-id
	function update_post($id,$title,$content,$cat_id,$state,$post_meta,$post_robots,$meta_title,$meta_description)
	{
		$query = "UPDATE posts SET title ='$title',content='$content',cat_id=$cat_id,state=$state,post_meta='$post_meta',post_robots='$post_robots',meta_title='$meta_title',meta_description='$meta_description' WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Delete posts with conditions
	function delete_posts($conditions)
	{
		$query = "DELETE FROM posts WHERE $conditions";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
?>